package fr.upstlse.glre.client.espacededie.commun.communiquer;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

import fr.upstlse.glre.client.constante.fields.MessageConstante;
import fr.upstlse.glre.client.espacededie.commun.communiquer.action.ViewerSelectionAction;
import fr.upstlse.glre.client.vo.user.UserVOGWT;

public class Message extends LayoutContainer {

	private UserVOGWT userVOGWT;
	
	private ContentPanel menu;
	private ContentPanel corps;
	
	private static Message instance;
	
	public Message() {
		instance = this;
	}

	@Override
	protected void onRender(Element pParent, int pIndex) {
		super.onRender(pParent, pIndex);
		setLayout(new BorderLayout());
		createCorps();
		BorderLayoutData lCorpsBorderLayoutData = new BorderLayoutData(LayoutRegion.CENTER);
		lCorpsBorderLayoutData.setMargins(new Margins(5,0,5,0));
		add(corps, lCorpsBorderLayoutData);
		
		createMenu();
		BorderLayoutData lMenuBorderLayoutData = new BorderLayoutData(LayoutRegion.WEST);
		lMenuBorderLayoutData.setCollapsible(true);
		lMenuBorderLayoutData.setSplit(true);
		lMenuBorderLayoutData.setMargins(new Margins(5));
		add(menu, lMenuBorderLayoutData);
	}

	private void createMenu() {
		menu = new ContentPanel();
		Label lMessageLabel = new Label();
		lMessageLabel.setText(MessageConstante.MENU_MESSAGE_LABEL_TEXT.getValue());
		lMessageLabel.addClickListener(new ClickListener() {

			public void onClick(Widget pArg0) {
				corps.removeAll();
				corps.layout();
				corps.setHeading(MessageConstante.MENU_MESSAGE_LABEL_TEXT.getValue());
				corps.add(new Ecrire());
				corps.layout();
			}
		});
		menu.add(lMessageLabel);
		
		Label lBoiteAuxLettresLabel = new Label();
		lBoiteAuxLettresLabel.setText(MessageConstante.MENU_BOITEAUXLETTRES_LABEL_TEXT.getValue());
		lBoiteAuxLettresLabel.addClickListener(new ClickListener() {

			public void onClick(Widget pArg0) {
				corps.setHeading(MessageConstante.MENU_BOITEAUXLETTRES_LABEL_TEXT.getValue());
				if((Lire.getInstance() != null) && (!Lire.getInstance().isVisible())){
					if(ViewerSelectionAction.getReadPanelInstance() != null) {
							corps.remove(ViewerSelectionAction.getReadPanelInstance());
					}
					Lire.getInstance().getLirePanel().setVisible(true);
				}
				else {
					corps.removeAll();
					corps.layout();
					Lire lLire = new Lire();
					lLire.setViewerSelectionAction(new ViewerSelectionAction());
					corps.add(lLire);
				}
				
				corps.layout();
			}
		});
		menu.add(lBoiteAuxLettresLabel);
	}
	
	private void createCorps() {
		corps = new ContentPanel();
		corps.setScrollMode(Scroll.AUTO);
		corps.add(new Ecrire());
	}

	public UserVOGWT getUserVOGWT() {
		return userVOGWT;
	}

	public void setUserVOGWT(UserVOGWT pUserVOGWT) {
		userVOGWT = pUserVOGWT;
	}

	public ContentPanel getMenu() {
		return menu;
	}

	public void setMenu(ContentPanel pMenu) {
		menu = pMenu;
	}

	public ContentPanel getCorps() {
		return corps;
	}

	public void setCorps(ContentPanel pCorps) {
		corps = pCorps;
	}

	public static Message getInstance() {
		if(instance == null) {
			new Message();
		}
		return instance;
	}
}
